<?php
/**
 * Copyright (c) 2016.  hangzhou BOYE .Co.Ltd. All rights reserved
 */

/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-12-19
 * Time: 18:39
 */

namespace app\admin\controller;


use app\src\admin\api\UserApi;
use app\src\admin\helper\AdminFunctionHelper;
use app\src\base\enum\StatusEnum;
use app\src\base\facade\AccountFacade;
use app\src\buyorder\logic\BuyOrderLogic;
use app\src\goods\logic\ProductLogic;
use app\src\platform\logic\PlatformLogic;
use app\src\powersystem\logic\AuthGroupAccessLogic;
use app\src\powersystem\logic\AuthGroupLogic;
use app\src\repairerApply\logic\RepairerApplyLogicV2;
use app\src\session\logic\LoginSessionLogic;
use app\src\task\logic\RoleTaskLogic;
use app\src\user\action\RegisterAction;
use app\src\user\action\UserLogoutAction;
use app\src\user\facade\DefaultUserFacade;
use app\src\user\logic\MemberConfigLogic;
use app\src\user\logic\MemberLogic;
use app\src\user\logic\UcenterMemberLogic;
use app\src\user\logic\UserGroupLogic;
use app\src\user\logic\UserMemberLogic;
use app\src\user\logic\VUserInfoLogic;
use app\src\user\logic\UserRelationLogic;
use app\src\user\action\RelationAction;
use app\src\user\logic\DriverLogicV2;
use app\src\user\logic\WorkerLogicV2;
use app\src\wallet\logic\WalletHisLogicV2;
use app\src\wallet\logic\WalletLogic;
use app\src\wallet\model\WalletHis;
use app\weixin\controller\User;
use think\exception\DbException;
use think\Request;
use think\Db;

class Buyorder extends Admin{
    public function index(){
        $map=[];
        $params=[];
        $startdatetime = $this->_param('startdatetime');
        $startdatetime = strtotime($startdatetime);
        $enddatetime   = $this->_param('enddatetime');
        $enddatetime   = strtotime($enddatetime);
        if(!empty($startdatetime) && !empty($enddatetime)){
            if($startdatetime === FALSE || $enddatetime === FALSE){
                $params = array('startdatetime' =>$startdatetime, 'enddatetime' =>$enddatetime);
                $map['bo.create_time'] = array( array('EGT', $startdatetime), array('ELT', $enddatetime), 'and');
                $startdatetime = date("Y-m-d h:i:s", $startdatetime);
                $enddatetime   = date("Y-m-d h:i:s", $enddatetime);
            }else{
                $params = array('startdatetime' => $startdatetime, 'enddatetime' =>$enddatetime);
                $map['bo.create_time'] = array( array('EGT', $startdatetime), array('ELT', $enddatetime), 'and');
                $startdatetime = date("Y-m-d h:i:s", $startdatetime);
                $enddatetime   = date("Y-m-d h:i:s", $enddatetime);
            }
        }
        if(UID!=1){
            $admin_uid=(new PlatformLogic())->getInfo(['id'=>UID]);
            $map['ur.upper_uid']=$admin_uid['info']['uid'];
        }else{
            $admin_uid=(new PlatformLogic())->queryNoPaging([]);
            $map['ur.upper_uid']=['in',array_column($admin_uid['info'],'uid')];
        }


        $this->assign('startdatetime',$startdatetime);
        $this->assign('enddatetime',$enddatetime);


        $p = $this->_param('p',0);
        $page = array('curpage' => $p, 'size' => 10);
        $order = "bo.create_time desc ";
        $result=(new BuyOrderLogic())->query_order($map,$page,$order,$params);

        $cars=array_column($result['info']['list'],'id');

        $this->assign('list',$result['info']['list']);
        $this->assign('show',$result['info']['show']);
        return $this -> boye_display();
    }



    //购买详情
    public function detail(){
        $buy_id=$this->_param('id');
        $wallet_his=(new WalletHisLogicV2())->queryNoPaging(['order'=>$buy_id]);
        if(empty($wallet_his)) $this->error('查询出错');
        $tem=[];
        foreach($wallet_his as $k=>$v){
            $tem[$k]=$v;
            $uid=$v['uid'];
            $member=(new MemberLogic())->getInfo(['uid'=>$uid]);
            $tem[$k]['member']=$member['info'];
        }
        $this->assign('wallet',$tem);
        //task
        $task=(new RoleTaskLogic())->queryNoPaging(['order'=>$buy_id]);
        if(!$task['status']) $this->error('查询出错');
        $temp=[];
        if(!empty($task['info'])){
        foreach($task['info'] as $k=>$v){
            $temp[$k]=$v;
            $member=(new MemberLogic())->getInfo(['uid'=>$uid]);
            $temp[$k]['member']=$member['info'];
        }
        }else{
            $temp=[];
        }
        $this->assign('task',$temp);

        return $this->boye_display();

    }

}